<?php
abstract class Retriever
{
	
	protected $startTime;
	
	
	protected $logData = array();
	/**
	 * @return RetrieveLog
	 */
	protected $retrieveLog;
	/**
	 * 
	 * Enter description here ...
	 * @var int;
	 */
	private $season_nefub_id;
	
	/**
	 * @var Season
	 */
	private $season;
	
	private $context;
	
	/**
	 * @var int
	 */
	protected $countRequests;
	
	public function __construct()
	{
		$this->season = Season::getInstance();
	}
	
	/**
	 * @return Season
	 */
	protected function getSeason()
	{
		return $this->season;
	}
	
	public function getNefubContents($url)
	{
		if(!isset($this->context))
		{
			$opts = array(
			  'http'=>array(
			    'method'=>"GET",
			    'header'=>"Accept-header: application/nl.floorball.app.API-v1+json\r\n" 
			  )
			);
			$this->context = stream_context_create($opts);
		}
		$this->countRequests++;
		return file_get_contents($url, false, $this->context);
	}
	
	public function clearLog()
	{
		$path = 'activelog.txt';
		$f = fopen($path, 'w+');
		fclose($f);
	}
	
	/**
	 * Write something to log file
	 * @param mixed $text
	 * @param int $level
	 */
	public static function put($text, $level = 0)
	{
		$mt = date('d-m-Y H:i:s');
		$output  = utf8_decode(print_r($text, true));

		while($level > 0)
		{
			$output .="\t";
			$level --;
		}

		$output = $mt ."\t" . $output;
		$path = 'activelog.txt';
		$mode = 'a+';
		if(!file_exists($path))
		{
			$mode ='w+';
		}
		$f = fopen($path, $mode);
		fwrite($f, $output ."\r\n");
		fclose($f);
		$output .= "<pre>".$output."</pre>\r\n";
	}
	
	/**
	 * Render all pages so they load fast
	 */
	public function clear_renders()
	{
		error_reporting(0);
		
		self::put("build main page: ".HOST . '/?clear=1');
		get_headers(HOST . '/?clear=1');

		self::put("build calendar ".HOST . '/agenda?clear=1');
		get_headers(HOST . '/agenda?clear=1');
		
		self::put("build scores ".HOST . '/uitslagen?clear=1');
		get_headers(HOST . '/uitslagen?clear=1');
		
		self::put("build scores ".HOST . '/uitslagen?clear=1');
		get_headers(HOST . '/speler?clear=1');

		$oSeason = $this->getSeason();			
		$aPoules = $oSeason->getPoules();
					
		foreach($aPoules as $oPoule)
		{
			$url = HOST .'/poule/'.$oPoule->nefub_id . '?clear=1';
			self::put("Maak pagina voor poule ".$oPoule->name);
			get_headers($url);

			$aTeams = $oPoule->getPouleTeams();
			foreach($aTeams as $oPouleTeam)
			{
				$url = HOST .'/team/'.$oPouleTeam->team_nefub_id . '?clear=1';
				self::put("Maak pagina voor team ".$oPouleTeam->getTeam()->name);
				get_headers($url);
			}
				
			
			
		}
		$aGames = Game::getAll();
		
		foreach($aGames as $oGame)
		{
			$url = HOST .'/wedstrijd/'.$oGame->nefub_id . '?clear=1';
			self::put("Maak pagina voor wedstrijd ".$oGame->nefub_id);
			get_headers($url);
		}
		
		$this->clearLocationRenders();
		
	}

	public function clearLocationRenders()
	{
		$query = "SELECT `id`, `name`
					FROM Location
					WHERE id > 1 AND nefub_id IS NOT NULL";
		$result = Database::query($query);
		$locations = Location::getAll();
		foreach($locations as $location)
		{
			if($location->nefub_id)
			{
				$url = HOST .'/locatie/'.$location->nefub_id. '?clear=1';
				self::put("Maak pagina voor locatie ".$location->name);
				get_headers($url);
			}
		}		
	}
	
	/**
	 * Enter description here ...
	 * @return void
	 */
	abstract public function retrieve();
}